package com.imut.oa.controller.user;

import com.imut.oa.common.BaseResponse;
import com.imut.oa.common.ResultUtils;
import com.imut.oa.util.ThreadLoaclUtil;
import com.imut.oa.pojo.dto.LeaveSubmitDTO;
import com.imut.oa.pojo.dto.CommonUserPageQueryDTO;
import com.imut.oa.service.LeaveService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@RestController
@RequestMapping("/user/leave")
@Slf4j
@Tag(name = "员工请假")
public class UserLeaveController {
    @Autowired
    private LeaveService leaveService;

    @PostMapping("/submit")
    @Operation(summary = "提交请假申请")
    public BaseResponse<String> submitLeave(@RequestBody LeaveSubmitDTO submitDTO) {
        log.info("用户提交请假申请：{}", submitDTO);
        leaveService.submitLeave(submitDTO);
        return ResultUtils.success("提交成功",  "提交成功");
    }
        @GetMapping ("/history")
        @Operation(summary = "获取请假记录")
        public BaseResponse<?> getLeaveHistory(@RequestParam("currentPage") int currentPage) {
            log.info("获取第 {} 页的请假记录", currentPage);
            CommonUserPageQueryDTO queryDTO = new CommonUserPageQueryDTO();
            queryDTO.setPage(currentPage);
            // 假设每页大小固定为 10
            queryDTO.setPageSize(5);
            // 获取当前用户 ID
            Long currentId = ThreadLoaclUtil.getCurrentId();
            queryDTO.setEmployeeId(currentId); // 假设 LeavePageQueryDTO 中有 employeeId 字段
            return ResultUtils.success(leaveService.getLeaveHistory(queryDTO));
        }
    @PostMapping("/cancel")
    @Operation(summary = "撤销请假申请")
    public BaseResponse<String> cancelLeave(@RequestParam("leaveId") Long leaveId) {
        log.info("用户撤销请假申请，请假ID：{}", leaveId);
        leaveService.cancelLeave(leaveId);
        return ResultUtils.success("撤销成功", "撤销成功");
    }
    @GetMapping("/check")
    @Operation(summary = "检查用户今日是否请假")
    public BaseResponse<Boolean> checkIsLeaveToday() {
        Long currentId = ThreadLoaclUtil.getCurrentId();
        //今天日期
        String day = LocalDate.now().toString();
        boolean isLeave = leaveService.checkIsLeaveToday(currentId,day);
        return ResultUtils.success(isLeave);
    }
}
